package com.donleo.word.controller;

import com.donleo.word.common.CommonResult;
import com.donleo.word.model.Word;
import com.donleo.word.service.WordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author liangd
 * @since 2021-01-11 18:15
 */
@RestController
@RequestMapping("/word")
public class WordController {

    @Autowired
    private WordService wordService;

    /**
     * 查询所有
     */
    @GetMapping("/findAll")
    public CommonResult findAll() {
        List<Word> list = wordService.list();
        return CommonResult.success(list);
    }

    /**
     * 根据用户编号查询
     */
    @GetMapping("/findWordByUserCode")
    public CommonResult findWordByUserCode(@RequestParam String userCode) {
        List<Word> list = wordService.findWordByUserCode(userCode);
        System.out.println(list);
        return CommonResult.success(list);
    }

    /**
     * 单个查询1
     */
    @GetMapping("/findById1/{id}")
    public CommonResult findById1(@PathVariable Integer id) {
        Word word = wordService.getById(id);
        return CommonResult.success(word);
    }

    /**
     * 单个查询2
     */
    @GetMapping("/findById2")
    public CommonResult findById2(@RequestParam Integer id) {
        Word word = wordService.getById(id);
        return CommonResult.success(word);
    }

    /**
     * 根据Id多个查询
     */
    @PostMapping("/findByIds")
    public CommonResult findByIds(@RequestBody Integer[] ids) {
        List<Integer> asList = new ArrayList<>();
        if (ids.length != 0) {
            asList = Arrays.asList(ids);
        }
        List<Word> words = wordService.listByIds(asList);
        return CommonResult.success(words);
    }

    /**
     * 新增
     */
    @PostMapping("/create")
    public CommonResult create(@RequestBody Word word) {
        System.out.println(word);
        return CommonResult.success(null);
    }

    /**
     * 更新
     */
    @PutMapping("/update")
    public CommonResult update(@RequestBody Word word) {
        System.out.println(word);
        return CommonResult.success(null);
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public CommonResult delete(@RequestParam Integer id) {
        System.out.println(id);
        return CommonResult.success(null);
    }
}
